package com.chaoyue.media.controller;


import com.chaoyue.base.constant.FileType;
import com.chaoyue.base.model.PageParams;
import com.chaoyue.base.model.PageResult;
import com.chaoyue.media.beans.dto.QueryMediaParamsDto;
import com.chaoyue.media.beans.dto.UploadFileParamsDto;
import com.chaoyue.media.beans.dto.UploadFileResultDto;
import com.chaoyue.media.beans.po.MediaFiles;
import com.chaoyue.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import static com.chaoyue.base.constant.FileType.PICTURE;

/**
 * @author Mr.M
 * @version 1.0
 * @description 媒资文件管理接口
 * @date 2022/9/6 11:29
 *
 * 111
 */
@SuppressWarnings({"all"})
@Api(value = "媒资文件管理接口", tags = "媒资文件管理接口")
@RestController
public class MediaFilesController {
    @Autowired
    MediaFileService mediaFileService;

    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody(required = false) QueryMediaParamsDto queryMediaParamsDto) {
        Long companyId = 1232141425L;
        return mediaFileService.queryMediaFiels(companyId, pageParams, queryMediaParamsDto);
    }

    @ApiOperation("上传图片")
    @PostMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto upload(@RequestPart("filedata") MultipartFile filedata,
                                      @RequestParam(value= "objectName",required=false) String objectName) throws IOException {

        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        uploadFileParamsDto.setFilename(filedata.getOriginalFilename());
        uploadFileParamsDto.setFileSize(filedata.getSize());
        uploadFileParamsDto.setFileType(PICTURE.getCode());

        //创建临时文件
        File tempFile = File.createTempFile("minio", ".temp");
        filedata.transferTo(tempFile);
        Long companyId = 1232141425L;
        uploadFileParamsDto.setLocalFilePath(tempFile.getAbsolutePath());
        uploadFileParamsDto.setCompanyId(companyId);
        uploadFileParamsDto.setObjectName(objectName);
        return mediaFileService.uploadFile(uploadFileParamsDto);
    }


}
